#include "monitokun.h"

bool monitokun::instance = false;
monitokun *monitokun::monito = NULL;

int HEIGHT = 22; 
int WEIGHT = 22;

int MOVE_LEFT = 1;
int MOVE_RIGTH = 2;
int MOVE_UP = 3;
int MOVE_DOWN = 4;

bool change_frame=false;

void monitokun::characterToLoad(char *img)
{
	SDL_Surface* loadImage = IMG_Load("resources/soldier.png");
	//player = SDL_LoadBMP("resources/mono.bmp");
	
	player = NULL;
	if( loadImage != NULL )
    {
        //Create an optimized surface
        player = SDL_DisplayFormat( loadImage );

        //Free the old surface
        SDL_FreeSurface( loadImage );

        //If the surface was optimized
        if( player != NULL )
        {
            //Color key surface
            SDL_SetColorKey( player, SDL_SRCCOLORKEY, SDL_MapRGB( player->format, 255, 0, 255 ) );
        }
    }
    //SDL_SetColorKey( player, SDL_SRCCOLORKEY, SDL_MapRGB( player->format, 255, 0, 255 ) );
	if(player == NULL)
	{
		printf("No player image loaded\n");
		printf("Could not open, %s",&img);
	}
}

void monitokun::draw(SDL_Surface *screen)
{
	//SDL_BlitSurface(this->player,NULL,screen, &this->offset);
	//SDL_BlitSurface(this->player,NULL,screen, &this->rect());
	SDL_BlitSurface(this->player,&this->rect(),screen, &this->offset);
}

void monitokun::update(SDL_Surface * screen)
{
	SDL_Flip(screen);
}

void monitokun::setFree()
{
	SDL_FreeSurface(this->player);
}

void monitokun::updateKeys()//Uint8 *keys)
{
	//If there's an event to handle
    if( SDL_PollEvent( &event ) )
    {
    }
	//check the key pressed
	keys = SDL_GetKeyState(NULL);
	if(keys[SDLK_UP]>0) 
	{
		printf("[UP]\n");
		this->offset.y -=1;//-=5;
		this->state = MOVE_UP;
	}
	if(keys[SDLK_DOWN]>0) 
	{
		printf("[DOWN]\n");
		this->offset.y +=1;//+=5;
		this->state = MOVE_DOWN;
	}
	if(keys[SDLK_LEFT]>0) 
	{
		printf("[LEFT]\n");
		this->offset.x -=1;//-=5;
		this->state = MOVE_LEFT;
	}
	if(keys[SDLK_RIGHT]>0) 
	{
		printf("[RIGHT]\n");
		this->offset.x +=1;//+=5;
		this->state = MOVE_RIGTH;
	}
	if(keys[SDLK_ESCAPE])
	{
		
	}
}

void monitokun::move(char *whatyouredoing)
{

}

SDL_Rect monitokun::rect()
{
	SDL_Rect rect;
	//rect.x=0;
	rect.y=0;
	rect.w=22;
	rect.h=22;

	//bool change_frame=false;

	if(loops<30)
	{
		loops++;
	}else{
		if(change_frame){
			change_frame=false;
		}else{
			change_frame=true;
		}
		loops=0;
	}

	if(change_frame){
		rect.x=22;
	}else{
		rect.x=0;
	}

	switch(state){
	case 1:
		//rect.x=0;
		rect.y=22;
		rect.w=22;
		rect.h=22;
		break;
	case 2:
		//rect.x=0;
		rect.y=0;
		rect.w=22;
		rect.h=22;
		break;
	case 3:
		//rect.x=0;
		rect.y=44;
		rect.w=22;
		rect.h=22;
		break;
	case 5:
		//rect.x=0;
		rect.y=66;
		rect.w=22;
		rect.h=22;
		break;
	}
	return rect;
}